package com.banzhi.shop.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.banzhi.shop.utils.ToastUtils;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import static com.banzhi.shop.MyApp.APP_ID;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";

    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, APP_ID);
        api.handleIntent(getIntent(), this);


    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {
        int code = resp.errCode;
        if (code == 0) {
            //显示充值成功的页面和需要的操作
            Intent intent = new Intent();
            intent.setAction("com.banzhi");
            this.sendOrderedBroadcast(intent, null);   //有序广播发送
            finish();
        }
        if (code == -1) {
            //错误
            ToastUtils.makeToastShort("支付失败");
            finish();
        }
        if (code == -2) {
            //用户取消
            ToastUtils.makeToastShort("支付取消");
            finish();
        }

    }

    @Override
    protected void onDestroy() {

        super.onDestroy();
    }
}